package com.andova;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.andova.net.upload.OnOssUploadResultListener;
import com.andova.net.upload.UploadBean;
import com.andova.sample.R;
import com.andova.toolkit.net.oss.OssUploadFile;
import com.andova.toolkit.util.ImageUtil;
import com.andova.util.GASystemCamera;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.andova.toolkit.util.FileSizeUtil.getFileSize;

/**
 * Created by Administrator on 2018-08-15.
 *
 * @author kzaxil
 * @since 1.0.0
 */
public class UploadActivity extends AppCompatActivity implements OnOssUploadResultListener {
    private OssUploadFile ossUploadFile;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_upload);
        ossUploadFile = new OssUploadFile(this);
        ossUploadFile.setOnOssUploadResultListener(this);
    }

    public void takePicture(View view) {
        GASystemCamera.takeCamera(this, 0);
    }

    public void uploadPicture(View view) {
        UploadBean bean = new UploadBean();
        bean.url = GASystemCamera.getCurrentPhotoPath();
        ossUploadFile.uploadImageWithFlag(bean, true, 0, 0, 200, 0);
    }

    public void compressPicture(View view) {
        System.out.println("origin bitmap size is " + getFileSize(GASystemCamera.getCurrentPhotoPath()));
        Observable.just(GASystemCamera.getCurrentPhotoPath())
                .map(new Function<String, String>() {
                    @Override
                    public String apply(String path) throws Exception {
                        return ImageUtil.scaleImage(path, 200);
                    }
                })
                .subscribeOn(Schedulers.io()) // 在io线程中处理图片压缩
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    private Disposable dis;

                    @Override
                    public void onSubscribe(Disposable d) {
                        dis = d;
                    }

                    @Override
                    public void onNext(String path) {
                        System.out.println("compress bitmap size is " + getFileSize(path));
                    }

                    @Override
                    public void onError(Throwable e) {
                        System.out.println(e.getMessage());
                        if (dis != null) dis.dispose();
                    }

                    @Override
                    public void onComplete() {
                        if (dis != null) dis.dispose();
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || requestCode != 0) return;
        ((ImageView) findViewById(R.id.iv_icon)).setImageBitmap(ImageUtil.getLocalBitmap(GASystemCamera.getCurrentPhotoPath()));
    }

    @Override
    public void uploadReady() {

    }

    @Override
    public void uploadComplete(List<UploadBean> originUploadList, List<UploadBean> remoteUploadList, int flag) {

    }

    @Override
    public void uploadComplete(UploadBean originBean, UploadBean remoteBean, int flag) {

    }

    @Override
    public void uploadError(@Nullable Throwable clientException, @Nullable String filePath) {

    }
}
